import { mapState } from 'vuex';
import { requestHttp, maccApi } from '@utils/index';
import i18n from '@/locales';

const authSysMixin = {
  computed: {
    ...mapState('Group', ['groupMap']),
    ...mapState('User', ['userInfo', 'tenantInfo']),
    groupId() {
      for (const gId in this.groupMap) {
        if (this.groupMap[gId].type === 'BUILDING') {
          return parseInt(gId);
        }
      }
      return null;
    },
    groupInfo() {
      return this.groupMap[this.groupId];
    },
  },
  methods: {
    async requestHttpAuth(paramObj = { replaces: {}, querys: {}, params: {} }) {
      let url = paramObj.api;
      const { params, method, replaces, querys } = paramObj;

      // replaces
      for (const q in replaces) {
        url = url.replace(`{${q}}`, replaces[q]);
      }

      // querys
      let queryStr = '';
      let index = 0;
      for (const q in querys) {
        if (index !== 0) {
          queryStr += '&';
        }
        index++;
        queryStr += `${q}=${querys[q]}`;
      }
      if (queryStr) {
        url += `?${queryStr}`;
      }

      const ajaxParam = {
        urlSuffix: url,
        httpMethod: method,
        bodyParam: params,
      };

      const res = await requestHttp({
        api: maccApi.SAM_TRANSFER_GROUP_POST_API,
        method: 'POST',
        params: ajaxParam,
        replaces: {
          group_id: this.groupId,
        },
      });

      if (!res.data) {
        res.data = {
          code: res.code,
          success: res.success,
          message: res.msg,
          msg: res.msg,
        };
      }

      return res;
    },
  },
};

export default authSysMixin;
